package yanShiRenWu;

import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @NettyDelayTask: netty 延时任务
 * @author: ZBoHang
 * @time: 2021/9/6 15:28
 */
public class NettyDelayTask implements TimerTask {

    boolean flag;

    public NettyDelayTask(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run(Timeout timeout) throws Exception {
        System.out.println("即将删除···");
        this.flag = false;
    }
}

class NettyDelayTaskTest {
    public static void main(String[] args) {
        NettyDelayTask delayTask = new NettyDelayTask(true);
        Timer timer = new HashedWheelTimer();
        timer.newTimeout(delayTask, 5, TimeUnit.SECONDS);
        int i = 1;
        while(delayTask.flag)
        {
            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(i + "秒过去了");
            i++;
        }
    }
}













































